/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.tozo.rest;

import hu.tozo.facade.AwsFacade;
import hu.tozo.facade.GlassfishFacade;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import hu.tozo.model.AmazonModel;
import hu.tozo.model.PerformanceModel;
import hu.tozo.service.ClusterController;
import hu.tozo.service.EC2Service;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;

/**
 * REST Web Service
 *
 */
@Path("amazon")
public class AmazonInstance {
    
    private List<AmazonModel>models;

    private void buildModels() throws Exception{
        EC2Service ec2 = new EC2Service();
        models=ec2.getAllInstance();
        /*for(long x=1;x<100;x++){
            models.add(new AmazonModel(x,"Name"+x,"State"+x,"PublicDNS"+x));
        }*/
        
 
    }
    
    public AmazonInstance() throws Exception {
        //buildModels();
    }

    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Path("start/")
    public void startInstance(String instance) throws Exception {
        System.out.println("Amazon startInstance...:"+instance);
        ClusterController cc = new ClusterController();
        cc.startNode();        
    }

    @POST
    @Consumes("application/x-www-form-urlencoded")
    @Path("stop/")
    public void stopInstance(String instance) throws Exception {
        System.out.println("Amazon stopInstance...:"+instance);
        ClusterController cc = new ClusterController();
        cc.stopNode();        
    }

    @GET
    @Path("load/")
    @Produces("application/json")
    public PerformanceModel load() {        
        //System.out.println("Amazon load() started...");
        int random = (int)(Math.random()*100);
        return new PerformanceModel(random);
    }
    @GET
    @Path("stat/")
    @Produces("application/json")
    public String stat() {        
        try {
            GlassfishFacade gf  = new GlassfishFacade();
            EC2Service      ec2 = new EC2Service();
            List instances = ec2.getAllClusterInstanceIP(16);
            for (Iterator it = instances.iterator(); it.hasNext();) {
                String publicDNS = (String) it.next();
                gf.setIpAddress(publicDNS);
                gf.runTask("CPULoad");
                String terheles = gf.getKimenet();
                float teher = Float.parseFloat(terheles);
                System.out.println("Kimenet:"+teher); 
             }
        } catch (Exception ex) {
            Logger.getLogger(AmazonInstance.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "OK";
    }    
    
    @GET
    @Produces("application/json")
    public List<AmazonModel> listInstances(@Context HttpServletResponse response) throws Exception {
        buildModels();
        System.out.println("Amazon listInstances started...");
        return models;
    }
        
}
